package cn.tedu.jdbc;

import cn.tedu.jdbc.utils.DbUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JDBCTest14 {
    public static void main(String[] args) {
        /*Connection conn = null;
        PreparedStatement ps = null;
        try {
            //1.注册驱动
            //2.获取连接
            conn = DbUtils.getConnection();
            //3.获取数据库操作对象
            //4.执行sql语句
            String sql = "delete from emp " +
                    "where empno = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,8888);
            //5.处理结果集
            int count = ps.executeUpdate();
            System.out.println("删除了" + count + "条记录");
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            //6.释放资源
            DbUtils.close(conn,ps,null);
        }*/
        /**
         * 需求：将员工标号为8888的员工删除
         * 使用try-with-resources语法（java7的新特性）
         */
        String sql = "delete from emp " +
                     "where empno = ?";
        try (Connection conn = DbUtils.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1,8888);
            int count = ps.executeUpdate();
            System.out.println("删除了" + count + "条记录");
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }
}
